# set up environment ----
library(data.table)
library(ggplot2)
library(cowplot)
library(parallel)
source("R/00-functions.R")
load("data/analysis_data.RData")
RNGkind("L'Ecuyer-CMRG")
set.seed(356024460)

# regressions ----
#   heterogeneity by gender ----
fmla_list <- list(ranking ~ electability_last * male_candidate)

#     main dataset ----
data_main <- respondent_candidate_data[main == 1]
id_main <- data_main[, unique(subject_id)]
n_main <- length(id_main)
data_list_main <- lapply(id_main, function(i) {
  data_main[subject_id == i]
})
models_main <- lapply(fmla_list, lm, data = data_main)
boot <- function(i) {
  boot_ids <- sample(n_main, n_main, replace = TRUE)
  boot_data <- rbindlist(lapply(boot_ids, function(i) {
    data_list_main[[i]]
  }))
  boot_models <- lapply(fmla_list, lm, data = boot_data)
  lapply(boot_models, coef)
}
boots_main <- mclapply(1:1000, boot, mc.cores = 6)
boot_coefs_main <- rbindlist(lapply(boots_main, function(y) as.data.table(rbind(y[[1]]))))

#     full dataset ----
data_full <- copy(respondent_candidate_data)
id_full <- data_full[, unique(subject_id)]
n_full <- length(id_full)
data_list_full <- lapply(id_full, function(i) {
  data_full[subject_id == i]
})
models_full <- lapply(fmla_list, lm, data = data_full)
boot <- function(i) {
  boot_ids <- sample(n_full, n_full, replace = TRUE)
  boot_data <- rbindlist(lapply(boot_ids, function(i) {
    data_list_full[[i]]
  }))
  boot_models <- lapply(fmla_list, lm, data = boot_data)
  lapply(boot_models, coef)
}
boots_full <- mclapply(1:1000, boot, mc.cores = 6)
boot_coefs_full <- rbindlist(lapply(boots_full, function(y) as.data.table(rbind(y[[1]]))))

# plots ----
electability_effects_main <- rbind(
  boot_coefs_main[, .(
    model = 1,
    type = "Men",
    est = mean(electability_last + `electability_last:male_candidate`),
    q025 = quantile(
      electability_last + `electability_last:male_candidate`, .025),
    q975 = quantile(
      electability_last + `electability_last:male_candidate`, .975))],
  boot_coefs_main[, .(
    model = 1,
    type = "Women",
    est = mean(electability_last),
    q025 = quantile(electability_last, .025),
    q975 = quantile(electability_last, .975))])
electability_effects_full <- rbind(
  boot_coefs_full[, .(
    model = 1,
    type = "Men",
    est = mean(electability_last + `electability_last:male_candidate`),
    q025 = quantile(
      electability_last + `electability_last:male_candidate`, .025),
    q975 = quantile(
      electability_last + `electability_last:male_candidate`, .975))],
  boot_coefs_full[, .(
    model = 1,
    type = "Women",
    est = mean(electability_last),
    q025 = quantile(electability_last, .025),
    q975 = quantile(electability_last, .975))])
electability_effects_main[, type := factor(type,
  levels = c("Men", "Women", "White/Man", "Straight/\nWhite/Man", "Other Candidates"))]
electability_effects_full[, type := factor(type,
  levels = c("Men", "Women", "White/Man", "Straight/\nWhite/Man", "Other Candidates"))]

# plots ----
theme_set(theme_bw() +
    theme(
      legend.position = "none",
      strip.text.x = element_blank(),
      strip.background = element_blank()))
x1 <- dcast(electability_effects_main[model == 1, .(type, est)],
  . ~ type, value.var = "est")
x1[, model := 1]
g2 <- ggplot(electability_effects_main[model == 1], aes(type, est)) +
  facet_grid(~ model, scales = "free_x") +
  geom_hline(yintercept = 0, linetype = 3) +
  geom_point() +
  geom_errorbar(aes(ymin = q025, ymax = q975), width = 0) +
  geom_segment(data = x1, aes(
    xend = "Women",
    yend = electability_effects_main$est[2],
    x = "Men",
    y = electability_effects_main$est[1])) +
  xlab("") + ylab("") +
  theme(strip.background = element_blank()) +
  scale_y_reverse()

x1 <- .03
y1 <- 0
y2 <- .12
n <- 1
h <- (1 - y2) / 1
cairo_pdf("figures/candidates-main-nonreversed.pdf", 4, 4)
cowplot::ggdraw() +
  draw_plot(g2, x1, y1 + 0 * h, 1 - x1, h) +
  draw_label("Salience Effect", x1/2, .5,
    angle = 90, size = 10) +
  draw_label("Electability Salience Decreases\nRankings of Women Candidates",
    x1 + (1 - x1) / 2, 1 - y2 * .4, size = 13)
dev.off()

x1 <- dcast(electability_effects_full[model == 1, .(type, est)],
  . ~ type, value.var = "est")
x1[, model := 1]
g2 <- ggplot(electability_effects_full[model == 1], aes(type, est)) +
  facet_grid(~ model, scales = "free_x") +
  geom_hline(yintercept = 0, linetype = 3) +
  geom_point() +
  geom_errorbar(aes(ymin = q025, ymax = q975), width = 0) +
  geom_segment(data = x1, aes(
    xend = "Women",
    yend = electability_effects_full$est[2],
    x = "Men",
    y = electability_effects_full$est[1])) +
  xlab("") + ylab("") +
  theme(strip.background = element_blank()) +
  scale_y_reverse()
x1 <- .03
y1 <- 0
y2 <- .12
n <- 1
h <- (1 - y2) / 1
cairo_pdf("figures/candidates-full-nonreversed.pdf", 4, 4)
cowplot::ggdraw() +
  draw_plot(g2, x1, y1 + 0 * h, 1 - x1, h) +
  draw_label("Salience Effect", x1/2, .5,
    angle = 90, size = 10) +
  draw_label("Benefits of Electability (All Respondents)",
    x1 + (1 - x1) / 2, 1 - y2 * .4, size = 13)
dev.off()

save(boot_coefs_main, file = "results/boots_coefs_main-nonreversed.RData")
